home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / dataconv / dbftosql.pas < prev    next >
Pascal/Delphi Source File  |  1995-12-22  |  11KB  |  415 lines

  1. { DBFtoSQL - 'Fixed up' by:  Pete Ness
  2.                              scream@primenet.com
  3.                              CID: 102347,710
  4.  
  5.  This program was not originally written by me, I got it's raw state
  6.  from CIS - user id # 71073,2552 (I'll fill in the name when I get it).
  7.  It wasn't quite what I was looking for, but had some potential.  I've
  8.  re-done just about everything since, but the original author deserve
  9.  credit for the original work - the BatchMove component started as his
  10.  idea.
  11.  
  12.  The original author used this to import DBF's to Interbase SQL.  There
  13.  are really no utilities out there that I've found to do this well -
  14.  thus I wrote my own.  I use Microsoft SQL 6.0 and needed to import
  15.  data from Foxpro files.  This was originally written to go from
  16.  DBF's to SQL only, but it should work to move data from any ODBC
  17.  source to any other ODBC source (through the BDE).  I'm still having
  18.  some problems with SQL 6.0 blocking the inserts after the first 75 -
  19.  150 records.  If anyone has this problem with this, let me know and
  20.  we'll figure out how to fix it.  I will also upload a fixed version
  21.  when I find the problem.}
  22.  
  23. unit DbfToSQL;
  24.  
  25. interface
  26.  
  27. uses
  28.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  29.   Forms, Dialogs, MemoForm, StdCtrls, DB, DBTables, Buttons, Gauges, ProgForm,
  30.   ExtCtrls, paredit;
  31.  
  32. type
  33.   TDBF2SQL = class(TForm)
  34.     CnvrtBtn: TButton;
  35.     ExitBtn: TButton;
  36.     SrcTable: TTable;
  37.     SrcData: TDataSource;
  38.     BatchMove: TBatchMove;
  39.     DestTable: TTable;
  40.     GroupBox1: TGroupBox;
  41.     Label1: TLabel;
  42.     SourcePath: TEdit;
  43.     DbfFile: TLabel;
  44.     GroupBox2: TGroupBox;
  45.     Label4: TLabel;
  46.     Label6: TLabel;
  47.     RadioGroup1: TRadioGroup;
  48.     Label2: TLabel;
  49.     DestPath: TEdit;
  50.     Label3: TLabel;
  51.     SourceAlias: TComboBox;
  52.     DestAlias: TComboBox;
  53.     SourceTableName: TComboBox;
  54.     DestTableName: TComboBox;
  55.     SourceDataBase: TDatabase;
  56.     DestDataBase: TDatabase;
  57.     SpeedButton1: TSpeedButton;
  58.     SpeedButton2: TSpeedButton;
  59.     procedure CnvrtBtnClick(Sender: TObject);
  60.     procedure ExitBtnClick(Sender: TObject);
  61.     procedure SourceTableNameChange(Sender: TObject);
  62.     procedure RadioGroup1Click(Sender: TObject);
  63.     procedure FormCreate(Sender: TObject);
  64.     procedure SourceAliasChange(Sender: TObject);
  65.     procedure DestAliasChange(Sender: TObject);
  66.     procedure SourceTableNameEnter(Sender: TObject);
  67.     procedure DestTableNameEnter(Sender: TObject);
  68.     procedure SpeedButton1Click(Sender: TObject);
  69.     procedure SpeedButton2Click(Sender: TObject);
  70.  
  71.   private
  72.     { Private declarations }
  73.     Procedure OpenSource;
  74.     Procedure SetColumnNames;
  75.   public
  76.     abort : Boolean;
  77.     { Public declarations }
  78.   end;
  79.  
  80. var
  81.   DBF2SQL: TDBF2SQL;
  82.   lastsourcealias, lastdestalias : string;
  83.  
  84. implementation
  85.  
  86. {$R *.DFM}
  87.  
  88. procedure TDBF2SQL.ExitBtnClick(Sender: TObject);
  89. begin
  90.    Dbf2Sql.Close
  91. end;
  92.  
  93. Procedure TDBF2SQL.OpenSource;
  94. var text : string;
  95. begin
  96.    SrcTable.Close;
  97.    SourceDataBase.Connected := False;
  98.  
  99.    If (SourceTableName.Text = '')
  100.       Then raise Exception.Create('Source Table Name is incomplete!');
  101.    if (SourceAlias.Text = '')
  102.       Then raise Exception.Create('Source Alias not specified!');
  103.  
  104.    { The order these are listed in the component is not the same as
  105.       the ORD() values therefore, this case is used instead of just
  106.       TBatchMode[...ItemIndex] }
  107.  
  108.    Case RadioGroup1.ItemIndex of
  109.       0 : BatchMove.Mode := batAppend;
  110.       1 : BatchMove.Mode := batAppendUpdate;
  111.       2 : BatchMove.Mode := batCopy;
  112.       3 : BatchMove.Mode := batDelete;
  113.       4 : BatchMove.Mode := batUpdate;
  114.    end;
  115.  
  116.    SrcTable.Active := False;
  117.    SourceDataBase.AliasName := SourceAlias.Text;
  118.    SrcTable.TableName := ExtractFileName(SourceTableName.Text);
  119.  
  120.    if SourcePath.Enabled and (sourcepath.text > '') then
  121.       SourceDataBase.Params.Add('PATH='+SourcePath.Text);
  122.  
  123.    try
  124.       chdir( SourcePath.Text );
  125.    except
  126.       Application.HandleException( self );
  127.    end;
  128.  
  129.    SrcTable.Open;
  130.    SourceDataBase.Connected := True;
  131.  
  132. end;
  133.  
  134. { Read the column names from the database.  Be aware that many "NORMAL"
  135.   DBF type field names are illegal under sequal.  I opted to not
  136.   automatically rename the fields as most of the time they are OK. }
  137.  
  138.  
  139. Procedure TDBF2SQL.SetColumnNames;
  140. Var MovFlds: TStringList;
  141.      I: integer;
  142.      SameDBF: Boolean;
  143.      Nm: String;
  144. Begin
  145.  
  146.    MovFlds := TStringList.Create;
  147.    If SrcTable.FieldCount > 0 then begin
  148.       If FieldAssign.ConvFields.Lines.Count > 0
  149.          Then SameDBF :=
  150.             (Pos(SrcTable.Fields[0].FieldName,FieldAssign.ConvFields.Lines[0])>0)
  151.  
  152.          Else SameDBF := FALSE;
  153.  
  154.       if SameDBF then
  155.          MovFlds.Assign(FieldAssign.ConvFields.Lines)
  156.       else
  157.          For I := 0 to SrcTable.FieldCount-1 do begin
  158.             Nm := SrcTable.Fields[I].FieldName;
  159.             MovFlds.Add(Nm+'='+Nm);
  160.          End;
  161.  
  162.    End Else
  163.  
  164.       raise Exception.Create('No Source Fields Found.');
  165.  
  166.    FieldAssign.ConvFields.Lines.Clear;
  167.    FieldAssign.ConvFields.Lines := MovFlds;
  168.  
  169.    if FieldAssign.ShowModal = mrCancel then
  170.       Abort := True
  171.    else
  172.       Abort := False;
  173.  
  174.    MovFlds.Free;
  175.  
  176. End;
  177.  
  178.  
  179. procedure TDBF2SQL.CnvrtBtnClick(Sender: TObject);
  180. var lastcount, ProcCount, ChangeCount, KeyCount, ConvCount : longint;
  181.    text : string;
  182.  
  183. Begin
  184.  
  185.    { Make sure the proper paths are setup in the parameters - just in
  186.      case the path was entered AFTER the table name field }
  187.  
  188.    SourceTableNameEnter(self);
  189.    DestTableNameEnter(self);
  190.  
  191.    OpenSource;
  192.    SrcTable.First;
  193.    If SrcTable.EOF then
  194.       raise Exception.Create('Source Table is Empty');
  195.    If DestTableName.Text = EmptyStr Then
  196.       raise Exception.Create('Destination Tablename is empty');
  197.    If DestAlias.Text = EmptyStr Then
  198.       raise Exception.Create('Destination Alias is empty');
  199.  
  200.    Abort := False;
  201.  
  202.    SetColumnNames;
  203.  
  204. End;
  205.  
  206. procedure TDBF2SQL.SourceTableNameChange(Sender: TObject);
  207. begin
  208.    FieldAssign.ConvFields.Lines.Clear
  209. end;
  210.  
  211. procedure TDBF2SQL.RadioGroup1Click(Sender: TObject);
  212. begin
  213.    BatchMove.Mode := TBatchMode(RadioGroup1.ItemIndex);   
  214. end;
  215.  
  216. procedure TDBF2SQL.FormCreate(Sender: TObject);
  217. begin
  218.  
  219.    Session.GetAliasNames(DestAlias.Items);
  220.    Session.GetAliasNames(SourceAlias.Items);
  221.    LastSourceAlias := '';
  222.    LastDestAlias := '';
  223.  
  224. end;
  225.  
  226. { The next routine is used by the following two - it is not called
  227.   From outside this form, so it's not a method of the form - although
  228.   it could be... }
  229.  
  230. Function FindPath(AliasName : String) : String;
  231. var TempStrings : TStringlist;
  232.     loop, foundloop, foundpos : integer;
  233.     pathstring : string;
  234. begin
  235.  
  236.    PathString := '';
  237.  
  238.    TempStrings := TStringlist.Create;
  239.    Session.GetAliasParams(AliasName,TempStrings);
  240.    foundloop := -1;
  241.  
  242.    for loop := 0 to TempStrings.Count-1 do begin
  243.       foundpos := pos('PATH=',uppercase(TempStrings[loop]));
  244.       if foundpos > 0 then begin
  245.          pathstring := copy(TempStrings[loop],foundpos+5,255);
  246.          foundloop := loop;
  247.       end;
  248.    end;
  249.  
  250.    TempStrings.Free;
  251.    FindPath := PathString;
  252.  
  253. end;
  254.  
  255. procedure TDBF2SQL.SourceAliasChange(Sender: TObject);
  256. begin
  257.  
  258.    if (SourceAlias.Text > '') and (LastSourceAlias <> SourceAlias.Text)
  259.           then begin
  260.  
  261.       SourcedataBase.Connected := False;
  262.  
  263.       SourceDataBase.AliasName := SourceAlias.Text;
  264.  
  265.       session.GetAliasParams(SourceAlias.Text, SourceDataBase.params);
  266.  
  267.       SourcePath.Text := FindPath(SourceAlias.Text);
  268.  
  269.       if SourcePath.Text = '' then begin
  270.          SourcePath.Enabled := True;
  271.       end else begin
  272.          SourcePath.Enabled := False;
  273.       end;
  274.  
  275.       LastSourceAlias := SourceAlias.Text;
  276.       SourceDataBase.Connected := True;
  277.  
  278.    end;
  279.  
  280.    LastSourceAlias := SourceAlias.Text;
  281.  
  282. end;
  283.  
  284. procedure TDBF2SQL.DestAliasChange(Sender: TObject);
  285. begin
  286.  
  287.    if (DestAlias.Text > '') and (LastDestAlias <> DestAlias.text) then begin;
  288.  
  289.       DestDataBase.Connected := False;
  290.  
  291.       DestDataBase.AliasName := DestAlias.Text;
  292.  
  293.       DestPath.Text := FindPath(DestAlias.Text);
  294.  
  295.       if DestPath.Text = '' then begin
  296.          DestPath.Enabled := True;
  297.       end else begin
  298.          DestPath.Enabled := False;
  299.       end;
  300.  
  301.       session.GetAliasParams(DestAlias.Text, DestDataBase.params);
  302.  
  303.       LastDestAlias := DestAlias.Text;
  304.       DestDataBase.Connected := True;
  305.  
  306.    end;
  307.  
  308.    LastDestAlias := DestAlias.Text;
  309.  
  310. end;
  311.  
  312. {* The Next two routines could probably use a little OO'ing as they
  313.    are identical except for field names... }
  314.  
  315. procedure TDBF2SQL.SourceTableNameEnter(Sender: TObject);
  316. var Inserted : Boolean;
  317.     loop : integer;
  318. begin
  319.  
  320.    SourceDataBase.Connected := False;
  321.  
  322.    Inserted := False;
  323.    If SourcePath.Enabled then begin
  324.       for loop := 0 to SourceDataBase.Params.Count - 1 do
  325.          if pos('PATH=',uppercase(SourceDataBase.Params[loop]))
  326.                > 0 then begin
  327.             Inserted := True;
  328.             if SourcePath.Text > '' then
  329.                SourceDataBase.Params[loop] := 'PATH='+SourcePath.Text
  330.             else
  331.                SourceDataBase.Params[loop] := '';
  332.          end;
  333.       if not Inserted then
  334.          SourceDataBase.Params.Add('PATH='+SourcePath.Text);
  335.  
  336.       try
  337.          chdir( SourcePath.Text );
  338.       except
  339.          Application.HandleException( self );
  340.       end;
  341.  
  342.    end;
  343.  
  344.    SourceDataBase.Connected := True;
  345.  
  346.    if SourceAlias.Text > '' then
  347.       Session.GetTableNames('SOURCEDB', '*.DBF' , True, False, SourceTableName.Items);
  348.  
  349. end;
  350.  
  351. procedure TDBF2SQL.DestTableNameEnter(Sender: TObject);
  352. var Inserted : Boolean;
  353.     loop : integer;
  354. begin
  355.  
  356.    DestDataBase.Connected := False;
  357.  
  358.    Inserted := False;
  359.    If DestPath.Enabled then begin
  360.       for loop := 0 to DestDataBase.Params.Count - 1 do
  361.          if pos('PATH=',uppercase(DestDataBase.Params[loop]))
  362.                > 0 then begin
  363.             Inserted := True;
  364.             if DestPath.Text > '' then
  365.                DestDataBase.Params[loop] := 'PATH='+DestPath.Text
  366.             else
  367.                DestDataBase.Params[loop] := '';
  368.          end;
  369.       if not Inserted then
  370.          DestDataBase.Params.Add('PATH='+DestPath.Text);
  371.       try
  372.          chdir( DestPath.Text );
  373.       except
  374.          Application.HandleException( self );
  375.       end;
  376.    end;
  377.    DestDataBase.Connected := True;
  378.  
  379.    if DestAlias.Text > '' then
  380.       Session.GetTableNames('DestDB', '', True, False, DestTableName.Items);
  381.  
  382. end;
  383.  
  384. procedure TDBF2SQL.SpeedButton1Click(Sender: TObject);
  385. begin
  386.  
  387.    SourceAliasChange(Self);
  388.  
  389.    DataParams.ParamEdit.Lines := SourceDataBase.params;
  390.  
  391.    if DataParams.ShowModal = mrOK then begin
  392.       SourceDataBase.Connected := False;
  393.       SourceDataBase.params := DataParams.ParamEdit.Lines;
  394.       SourceDataBase.Connected := True;
  395.    end;
  396.  
  397. end;
  398.  
  399. procedure TDBF2SQL.SpeedButton2Click(Sender: TObject);
  400. begin
  401.  
  402.    DestAliasChange(Self);
  403.  
  404.    DataParams.ParamEdit.Lines := DestDataBase.params;
  405.  
  406.    if DataParams.ShowModal = mrOK then begin
  407.       DestDataBase.Connected := False;
  408.       DestDataBase.params := DataParams.ParamEdit.Lines;
  409.       DestDataBase.Connected := True;
  410.    end;
  411.  
  412. end;
  413.  
  414. end.
  415.